package com.kwq.designPattern.exam;

import java.util.Scanner;

/**
 * @author kangwanqiang
 * @date 2021/3/27
 */
public class Mihayou {
    static class TreeNode {
        int val;
        TreeNode left = null;
        TreeNode right = null;

        TreeNode(int val) {
            this.val = val;
        }
    }

    public static TreeNode buildTree(String str) {
        if (str == null) {
            return null;
        }
        int val = Integer.parseInt(str.substring(0, str.indexOf("(")));
        String[] arr = str.split(",");
        int mid = arr.length / 2;
        int pos = 0;
        int index = 0;
        for (char c : str.toCharArray()) {
            if (c == ',') {
                if (index == mid-1) {
                    break;
                } else {
                    index++;
                    pos++;
                }
            }
            pos++;
        }
        String left = str.substring(str.indexOf("(")+1, pos-1);
        String right = str.substring(pos, str.lastIndexOf(")"));
        TreeNode root = new TreeNode(val);
        root.left = buildTree(left);
        root.right = buildTree(right);
        return root;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        buildTree(s);
    }
}
